<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <script src="./lib/vue-2.4.0.js"></script>
  <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css" />
  <script src="./lib/vue-resource-1.3.4.js"></script>
</head>

<body>
  <div id="app">
    <div class="panel panel-primary">
      <div class="panel-heading">
        <h3 class="panel-title">添加品牌</h3>
      </div>
      <div class="panel-body form-inline">
        <label>
          Name: <input type="text" v-model="name" class="form-control" />
        </label>
        <input type="button" value="添加" @click="add" class="btn btn-primary" />
      </div>
    </div>

    <table class="table table-striped table-hover table-bordered">
      <thead class="thead-inverse">
        <tr>
          <th>id</th>
          <th>name</th>
          <th>Crimt</th>
          <th>Operation</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in list" :key="item.id">
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.crimt }}</td>
          <td><a href="" @click.prevent="del(item.id)">删除</a></td>
        </tr>
      </tbody>
    </table>
  </div>
  <script>
    Vue.http.options.root="http://vue.studyit.io/"
    var vm = new Vue({
      el: "#app",
      data: {
        name: "",

        list: [{
            //存放所有品牌列表的数组
            id: 1,
            name: "宝马",
            crimt: new Date()
          },
          {
            id: 2,
            name: "五菱宏光",
            crimt: new Date()
          }
        ]
      },
      created() {
        this.getAllList()
      },
      methods: {
        add() {
          this.$http.post("api/addproduct", {
            name: this.name
          }, {
            emulateJSON: true
          }).then(result => {
            if (result.body.status === 0) {
              this.getAllList(),
                this.name = ''
            } else {
              alert("失败了！")
            }
          })
        },
        getAllList() {
          // 获取所有的品牌列表
          this.$http.get("api/getprodlist")
            .then(result => {
              var result = result.body
              if (result.status == 0) {
                this.list = result.massage;
              } else {
                alert("获取数据失败");
              }
            });
        },
        del(id) {
          this.$http.get("api/delproduct/" + id).then(result => {
              if(result.body.status===0){
                this.getAllList()
              }
              else{
                alert("删除失败！")
              }
          })
        }

      }
    });
  </script>
</body>

</html>